<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class Controllers {
	public function PreLoad($controller) {
		global $Debug, $Hooks;
		$path = APPLICATION_PATH.APP_TYPE."/".APPLICATION."/controllers/{$controller}.php";
		if(file_exists($path)) {
			$Hooks->run_hooks("controller_preloaded_pre");
			require_once($path);
			$Hooks->run_hooks("controller_preloaded");
			$Debug->writeLog("Initialized Controller {$controller} in Application ".APPLICATION.".", "Core");
			$controller = ucwords($controller);
			$Hooks->run_hooks("controller_preload_assign");
			$this->Controller = new $controller;
		}
		else {
			return false;
		}
	}
	
	public function PreLoadedRun($inputs = array(), $action = "Index") {
		if($action != "Index" and $action !== false) {
			if(substr($action, 0, 1) == "_") { // Internal functions are not for you silly
				return false;
			}
			else {
				$action = ucwords($action);
				if(method_exists($this->Controller, $action)) {
					if(count($inputs) == 0)
						$this->Controller->{$action}();
					else
						$this->Controller->{$action}($inputs);
					return true;
				}
				else{
					// removed @since revision 02
					// $this->Controller->index();
					return false;
				}
			}
		}
		else {
			if(count($inputs) == 0)
				$this->Controller->Index();
			else
				$this->Controller->Index($inputs);
			return true;
		}
	}
	
	public function Load($controller, $inputs = array(), $action = "Index") {
		$path = APPLICATION_PATH.APP_TYPE."/".APPLICATION."/controllers/{$controller}.php";
		if(file_exists($path)) {
			require_once($path);
			$controller = ucwords($controller);
			$this->Controller = new $controller;
			if($action != "Index") {
				if(substr($action, 0, 1) == "_") { // Internal functions are not for you silly
					return false;
				}
				else {
					if(method_exists($this->Controller, $action)) {
						if(count($inputs) == 0)
							$this->Controller->{$action}();
						else
							$this->Controller->{$action}($inputs);
						return true;
					}
					else{
						// removed @since revision 02
						// $this->Controller->index();
						return false;
					}
				}
			}
			else {
				if(count($inputs) == 0)
					$this->Controller->Index();
				else
					$this->Controller->Index($inputs);
				return true;
			}
		}
		else {
			return false;
		}
	}
}
					
class CF_Controller{
	// CI-like Loader Shortcuts should be done soon
}
	
